// Scintilla source code edit control
/** @file CharClassify.cxx
 ** Character classifications used by Document and RESearch.
 **/
// Copyright 2006 by Neil Hodgson <neilh@scintilla.org>
// The License.txt file describes the conditions under which this software may be distributed.

#include <stdlib.h>
#include <ctype.h>

#include "CharClassify.h"

CharClassify::CharClassify() {
	SetDefaultCharClasses(true);
}

void CharClassify::SetDefaultCharClasses(bool includeWordClass) {
	// Initialize all WCHAR classes to default values
	for (int ch = 0; ch < 256; ch++) {
		if (ch == L'\r' || ch == L'\n')
			charClass[ch] = ccNewLine;
		else if (ch < 0x20 || ch == L' ')
			charClass[ch] = ccSpace;
		else if (includeWordClass && (ch >= 0x80 || isalnum(ch) || ch == L'_'))
			charClass[ch] = ccWord;
		else
			charClass[ch] = ccPunctuation;
	}
}

void CharClassify::SetCharClasses(const WCHAR *chars, cc newCharClass) {
	// Apply the newCharClass to the specifed chars
	if (chars) {
		while (*chars) {
			charClass[*chars] = static_cast<WCHAR>(newCharClass);
			chars++;
		}
	}
}

int CompareCaseInsensitive(const WCHAR *a, const WCHAR *b) {
	while (*a && *b) {
		if (*a != *b) {
			WCHAR upperA = MakeUpperCase(*a);
			WCHAR upperB = MakeUpperCase(*b);
			if (upperA != upperB)
				return upperA - upperB;
		}
		a++;
		b++;
	}
	// Either *a or *b is nul
	return *a - *b;
}

int CompareNCaseInsensitive(const WCHAR *a, const WCHAR *b, size_t len) {
	while (*a && *b && len) {
		if (*a != *b) {
			WCHAR upperA = MakeUpperCase(*a);
			WCHAR upperB = MakeUpperCase(*b);
			if (upperA != upperB)
				return upperA - upperB;
		}
		a++;
		b++;
		len--;
	}
	if (len == 0)
		return 0;
	else
		// Either *a or *b is nul
		return *a - *b;
}
